package microBlogger.screens;

import context.*;


import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Ticker;

import microBlogger.MicroBlogger;

import bloggerAPI.Blog;
import bloggerAPI.BlogEntry;
import bloggerAPI.j2me.BloggerConnection;

import htmlCanvas.HtmlCanvas;

// ******************************************************************
// *** CLASS: EntryListScreen
// ******************************************************************
public class EntryListScreen extends List implements Debug.Constants
{ //private static final boolean DEBUG=true; // Overriding Debug.Constants.DEBUG
  private static final Command backCommand=new Command("Back",Command.BACK,1);
  private static final Command newCommand=new Command("New",Command.SCREEN,1);
  private static final Command editCommand=new Command("Edit",Command.SCREEN,1);
  private static final Command deleteCommand=new Command("Delete",Command.SCREEN,1);

  private MicroBlogger midlet;
  private Display display;
  private Displayable prev;
  private BloggerConnection bloggerConnection;
  private Blog blog;
  private Vector entryVector; // Vector of BlogEntry
  

  public EntryListScreen(MicroBlogger midlet,BloggerConnection bloggerConnection,Blog blog)
  { super("EntryList:",List.IMPLICIT);
    this.midlet=midlet;
    midlet.setEntryListScreen(this);
    this.bloggerConnection=bloggerConnection;
    this.blog=blog;
    this.display=Display.getDisplay(midlet);
    this.addCommand(backCommand);
    this.addCommand(newCommand);
    this.addCommand(editCommand);
    this.addCommand(deleteCommand);
    this.setCommandListener(commandListener);
  }

  
  private CommandListener commandListener=new CommandListener()
  { public void commandAction(Command c,Displayable s)
    { if(c==backCommand) { display.setCurrent(prev); }
      else if(c==newCommand) addEntry();
      else if(c==editCommand) editEntry();
      else if(c==deleteCommand) deleteEntry();
      else if(c==SELECT_COMMAND) showInBrowser();
    }
  };
  
  private BlogEntry selectedEntry()
  { return((BlogEntry)entryVector.elementAt(getSelectedIndex())); }
  
  public void show()
  { if(prev==null) prev=display.getCurrent();
    display.setCurrent(this);
    loadEntries();
  }
  
  private void showInBrowser()
  { new Thread("HtmlBrowser")
	{ public void run()
	  { HtmlCanvas htmlCanvas=new HtmlCanvas(midlet);
        htmlCanvas.show();
        String html="<HTML><BODY>"+selectedEntry().getText()+"</BODY></HTML>";
        htmlCanvas.openPage(html);
	  }
	}.start();
  }
  
  private void loadEntries()
  { new Thread("Loading")
  	{ public void run()
  	  { try
        { setTicker(new Ticker("... Loading "+blog.getTitle()+" Entries ..."));	
  	      entryVector=bloggerConnection.loadBlogEntries(blog);
  	      deleteAll();
  	      for(Enumeration e=entryVector.elements();e.hasMoreElements();)
  	      { BlogEntry entry=(BlogEntry)e.nextElement();
  	        EntryListScreen.this.append(entry.getTitle(),null);
  	      }
  	      setTicker(null);
        }
        catch(IOException e) { Debug.EXCEPTION("BlogListScreen'commandListener",e); }
  	  }
  	}.start();
  }
  
  private void addEntry()
  { BlogEntry newEntry=new BlogEntry(blog,"","");
    new EntryScreen(midlet,bloggerConnection,newEntry,false).show();
  }

  private void editEntry()
  { new EntryScreen(midlet,bloggerConnection,selectedEntry(),true).show(); }
  
  private void deleteEntry()
  { new Thread("Deleting")
	{ public void run()
	  { try
	    { BlogEntry entry=selectedEntry();
		  setTicker(new Ticker("... Deleting "+entry.getTitle()+" ..."));
    	  bloggerConnection.deleteBlogEntry(entry);
    	  setTicker(null);
   	      midlet.getEntryListScreen().show();
	  	}
	    catch(IOException e) { Debug.EXCEPTION("MicroBlogger.connect",e); }
	  }
	}.start();
  }
  
}
